<template>
  <div class="xtx-goods-page" v-if="goods">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="`/category/${goods.categories[1].id}`">{{goods.categories[1].name}}</XtxBreadItem>
        <XtxBreadItem :to="`/category/sub/${goods.categories[0].id}`">{{goods.categories[0].name}}</XtxBreadItem>
        <XtxBreadItem to="/">{{goods.name}}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <div class="media">
          <GoodsImage :images="goods.mainPictures"></GoodsImage>
          <GoodsSales></GoodsSales>
        </div>
        <div class="spec">
          <!-- 名字区组件 -->
          <GoodsName :goods="goods"></GoodsName>
          <!-- 规格组件 -->
          <GoodsSku :goods="goods" @change="changeSku"></GoodsSku>
          <!-- 数量选择组件 -->
          <XtxNumbox v-model="num" :max="goods.inventory" :label="true"></XtxNumbox>
          <!-- 按钮组件 -->
          <XtxButton @click="addCart()" size="large" type="primary" style="margin-top:20px">加入购物车</XtxButton>
        </div>
      </div>
      <!-- 商品推荐 -->
      <GoodsRelevant></GoodsRelevant>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <GoodsTabs></GoodsTabs>
          <!-- 注意事项 -->
          <GoodsWarn></GoodsWarn>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot :type="1"></GoodsHot>
          <GoodsHot :type="2"></GoodsHot>
          <GoodsHot :type="3"></GoodsHot>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref } from '@vue/reactivity'
import GoodsRelevant from './components/goods-relevant'
import GoodsImage from './components/goods-image'
import GoodsSales from './components/goods-sales.vue'
import GoodsName from './components/goods-name.vue'
import GoodsSku from './components/goods-sku.vue'
import XtxNumbox from '@/components/library/xtx-numbox.vue'
import XtxButton from '@/components/library/xtx-button.vue'
import GoodsTabs from './components/goods-tabs.vue'
import GoodsHot from './components/goods-hot.vue'
import GoodsWarn from './components/goods-warn.vue'
import { findGoods } from '@/api/product'
import { useRoute } from 'vue-router'
import { nextTick, watch, provide } from '@vue/runtime-core'
import { useStore } from 'vuex'
import Message from '@/components/library/Message'
export default {
  name: 'XtxGoodsPage',
  components: { GoodsRelevant, GoodsImage, GoodsSales, GoodsName, GoodsSku, GoodsTabs, GoodsHot, GoodsWarn, XtxNumbox, XtxButton },
  setup () {
    // 1.获取商品详情，进行渲染
    const goods = useGoods()
    const changeSku = (sku) => {
      // 修改商品的现价 原价 库存信息
      if (sku.skuId) {
        goods.value.price = sku.price
        goods.value.oldPrice = sku.oldPrice
        goods.value.inventory = sku.inventory
      }
      // 记录选择后的sku 可能有数据也可能是空对象
      currsku.value = sku
      // console.log(sku)
    }
    // 提供goods数据给后代组件使用
    provide('goods', goods)
    // 选择的数量
    const num = ref(1)

    // 加入购物车
    const store = useStore()
    // 记录当前sku
    const currsku = ref(null)
    const addCart = () => {
      if (currsku.value && currsku.value.skuId) {
        const { skuId, specsText: attrsText, inventory: stock } = currsku.value
        const { id, name, price, mainPictures } = goods.value
        // 规格选择完整
        store.dispatch('cart/addCart', {
          skuId,
          attrsText,
          stock,
          id,
          name,
          price,
          nowPrice: price,
          picture: mainPictures[0],
          selected: true,
          isEffective: true,
          count: num.value
        })
      } else {
        // 规格选择不完整
        Message({ text: '请选择完整规格' })
      }
    }
    return { goods, changeSku, num, addCart }
  }
}
// 获取商品详情的函数
const useGoods = () => {
  // 出现路由地址的商品id发生变化，但是不会重新初始化组件
  const goods = ref(null)
  const route = useRoute()
  watch(() => route.params.id, (newval) => {
    if (newval && `/product/${newval}` === route.path) {
      findGoods(route.params.id).then(data => {
      // console.log(data.result)
      // 让商品数据为空，让使用v-if的组件可以重新销毁和创建，
        goods.value = null
        nextTick(() => {
          goods.value = data.result
        })
      })
    }
  }, { immediate: true })
  return goods
}
</script>

<style lang="less" scoped>
.goods-info{
  min-height: 600px;
  background: #fff;
  display: flex;
  .media{
    width: 580px;
    height: 600px;
    padding: 30px 50px;
    // background-color: #aaa;
  }
  .spec{
    flex:1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer{
  display: flex;
  margin-top: 20px;
  .goods-article{
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside{
    width: 280px;
    min-height: 1000px;
  }
}
/* .goods-tabs{
  min-height: 600px;
  background: #fff;
}
.goods-warn{
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
} */
</style>
